package co.yixiang.modules.pay;
import co.yixiang.api.ApiResult;
import co.yixiang.modules.pay.weixin.WeiXinUtil;
import co.yixiang.modules.pay.weixin.WxRefundData;
import co.yixiang.modules.pay.zhifubao.AlipayConfig;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author
 * @Date2020/6/29 11:42
 * @Version V1.0
 **/
@Slf4j

public class PayUtil {

    public static void main(String[] args) {
        refund(2,"chongzhi20210311093508681","4200000989202103116205635338",
                new BigDecimal("200"),new BigDecimal("200"));
    }

    public static ApiResult refund(Integer payType, String outTradeNum, String returnPayId,
                                   BigDecimal payAmount, BigDecimal refundAmount) {
        //1 支付寶  2 微信
        if (payType.equals(1)) {
            try {
                final AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.ali_url, AlipayConfig.APPID
                        , AlipayConfig.private_key, "json", "UTF-8"
                        , AlipayConfig.ali_public_key, AlipayConfig.sign_type);

                AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
                Map map = new HashMap(4);
                map.put("out_trade_no", outTradeNum);
                //支付宝返回交易流水号
                map.put("trade_no", returnPayId);
                map.put("out_request_no", "USD111");
                //退款金额
                map.put("refund_amount", refundAmount);
                //退款原因
                map.put("refund_reason", "取消订单");
                JSONObject jsonObj = JSONObject.fromObject(map);
                log.info("map.toString():" + jsonObj.toString());
                request.setBizContent(jsonObj.toString());
                //请求支付宝，执行退款
                AlipayTradeRefundResponse response = alipayClient.execute(request);
                if (response.isSuccess()) {
                    log.info("支付宝退款调用成功！");
                } else {
                    log.info("支付宝退款调用失败！");
                    return ApiResult.fail("支付宝退款调用失败");
                }
            } catch (Exception e) {
                log.info("支付宝退款调用失败！" + e.getMessage());
                return ApiResult.fail("支付宝退款调用失败");
            }
            //微信退款
        } else if (payType.equals(2)) {

            WxRefundData wxPaySendData = null;
            try {
                wxPaySendData = new WxRefundData(WeiXinUtil.WEIXIN_APP_ID,
                        WeiXinUtil.WEIXIN_MCH_ID, UUID.randomUUID().toString().replace("-", ""),
                        outTradeNum, UUID.randomUUID().toString().replace("-", ""),
                        payAmount.multiply(new BigDecimal(100)).intValue(),
                        refundAmount.multiply(new BigDecimal(100)).intValue(), WeiXinUtil.WEIXIN_MCH_ID);

                log.info("微信退款成功");
            } catch (Exception e) {
                log.info("微信退款失败");
                e.printStackTrace();
            }
            WeiXinUtil.refund(wxPaySendData, WeiXinUtil.WEIXIN_PAY_KEY);
        }

        return ApiResult.ok(null);
    }
}
